const axios = require('axios');

async function debugComment() {
  try {
    console.log('🔍 开始调试评论功能...\n');
    
    // 1. 测试服务器连接
    console.log('1️⃣ 测试服务器连接...');
    try {
      const pingResponse = await axios.get('http://localhost:3000/api/dynamic/list/user1');
      console.log('✅ 服务器连接正常');
    } catch (error) {
      console.log('❌ 服务器连接失败:', error.message);
      return;
    }
    
    // 2. 测试评论API
    console.log('\n2️⃣ 测试评论API...');
    const commentData = {
      userId: 'user1',
      dynamicId: 'd1',
      content: '这是一条调试评论'
    };
    
    console.log('📤 发送评论数据:', JSON.stringify(commentData, null, 2));
    
    const response = await axios.post('http://localhost:3000/api/dynamic/comment', commentData, {
      headers: {
        'Content-Type': 'application/json'
      },
      timeout: 10000
    });
    
    console.log('✅ 评论成功!');
    console.log('📥 响应数据:', JSON.stringify(response.data, null, 2));
    
    // 3. 验证评论是否保存
    console.log('\n3️⃣ 验证评论是否保存...');
    const verifyResponse = await axios.get('http://localhost:3000/api/dynamic/list/user1');
    const dynamics = verifyResponse.data.data;
    const targetDynamic = dynamics.find(d => d.id === 'd1');
    
    if (targetDynamic) {
      console.log('📊 动态信息:');
      console.log('  - 评论数量:', targetDynamic.comment_count);
      console.log('  - 评论列表长度:', targetDynamic.comments ? targetDynamic.comments.length : 0);
      
      if (targetDynamic.comments && targetDynamic.comments.length > 0) {
        console.log('📝 最新评论:');
        const latestComment = targetDynamic.comments[targetDynamic.comments.length - 1];
        console.log('  - 内容:', latestComment.content);
        console.log('  - 用户:', latestComment.user_name);
        console.log('  - 时间:', latestComment.create_time);
      }
    }
    
  } catch (error) {
    console.error('❌ 调试失败:');
    if (error.response) {
      console.error('  - 状态码:', error.response.status);
      console.error('  - 响应数据:', error.response.data);
    } else if (error.request) {
      console.error('  - 请求错误:', error.request);
    } else {
      console.error('  - 错误信息:', error.message);
    }
  }
}

debugComment(); 